package cn.stylefeng.guns.modular.character.controller;

import cn.stylefeng.guns.modular.character.entity.Character;
import cn.stylefeng.guns.modular.character.service.ICharacterService;
import cn.stylefeng.guns.sys.core.log.LogObjectHolder;
import cn.stylefeng.roses.core.base.controller.BaseController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * 角色管理控制器
 *
 * @author fengshuonan
 * @Date 2020-06-21 16:53:36
 */
@Controller
@RequestMapping("/character")
public class CharacterController extends BaseController {

    private String PREFIX = "/character/";

    @Autowired
    private ICharacterService characterService;

    /**
     * 跳转到角色管理首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "character.html";
    }

    /**
     * 跳转到添加角色管理
     */
    @RequestMapping("/character_add")
    public String characterAdd() {
        return PREFIX + "character_add.html";
    }

    /**
     * 跳转到修改角色管理
     */
    @RequestMapping("/character_update/{characterId}")
    public String characterUpdate(@PathVariable Integer characterId, Model model) {
        Character character = characterService.getById(characterId);
        model.addAttribute("item",character);
        LogObjectHolder.me().set(character);
        return PREFIX + "character_edit.html";
    }

    /**
     * 获取角色管理列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        return characterService.list();
    }

    /**
     * 新增角色管理
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Character character) {
        characterService.save(character);
        return SUCCESS_TIP;
    }

    /**
     * 删除角色管理
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer characterId) {
        characterService.removeById(characterId);
        return SUCCESS_TIP;
    }

    /**
     * 修改角色管理
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Character character) {
        characterService.updateById(character);
        return SUCCESS_TIP;
    }

    /**
     * 角色管理详情
     */
    @RequestMapping(value = "/detail/{characterId}")
    @ResponseBody
    public Object detail(@PathVariable("characterId") Integer characterId) {
        return characterService.getById(characterId);
    }
}
